package com.example.backed.service.impl;

import com.example.backed.dto.ProductQuery;
import com.example.backed.dto.ProductRequest;
import com.example.backed.entity.Product;
import com.example.backed.mapper.ProductMapper;
import com.example.backed.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl implements ProductService {

    private final ProductMapper productMapper;

    @Override
    @Transactional
    public Product createProduct(ProductRequest request) {
        Product product = new Product();
        product.setName(request.getName());
        product.setCategoryId(request.getCategoryId());
        product.setPrice(request.getPrice());
        product.setStock(request.getStock());
        product.setDescription(request.getDescription());
        product.setImage(request.getImage());
        product.setStatus(request.getStatus());
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());

        productMapper.insert(product);
        return getProduct(product.getId());
    }

    @Override
    @Transactional
    public Product updateProduct(Long id, ProductRequest request) {
        Product product = getProduct(id);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }

        product.setName(request.getName());
        product.setCategoryId(request.getCategoryId());
        product.setPrice(request.getPrice());
        product.setStock(request.getStock());
        product.setDescription(request.getDescription());
        product.setImage(request.getImage());
        product.setStatus(request.getStatus());
        product.setUpdateTime(LocalDateTime.now());

        productMapper.update(product);
        return getProduct(id);
    }

    @Override
    @Transactional
    public void deleteProduct(Long id) {
        Product product = getProduct(id);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        productMapper.deleteById(id);
    }

    @Override
    public Product getProduct(Long id) {
        return productMapper.findById(id);
    }

    @Override
    public List<Product> listProducts(ProductQuery query) {
        return productMapper.findByQuery(query);
    }

    @Override
    public long countProducts(ProductQuery query) {
        return productMapper.countByQuery(query);
    }

    @Override
    @Transactional
    public boolean updateStock(Long id, Integer stock) {
        Product product = getProduct(id);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }

        // 检查库存是否充足（当减少库存时）
        if (stock < 0 && product.getStock() + stock < 0) {
            throw new RuntimeException("商品库存不足");
        }

        return productMapper.updateStock(id, stock) > 0;
    }
} 